OpenLayers.Control.Click = OpenLayers.Class(OpenLayers.Control, {
    defaultHandlerOptions: {
        'single': true,
        'double': false,
        'pixelTolerance': 0,
        'stopSingle': false,
        'stopDouble': false
    },

    initialize: function (options) {
        this.handlerOptions = OpenLayers.Util.extend(
            {}, this.defaultHandlerOptions
        );
        OpenLayers.Control.prototype.initialize.apply(
            this, arguments
        );
        this.handler = new OpenLayers.Handler.Click(
            this, {
                'click': this.trigger
            }, this.handlerOptions
        );
    },

    trigger: function (e) {
        var lonlat = map.getLonLatFromPixel(e.xy);
        var username_api = "co.marios";



        $.ajax({
            url: "http://api.geonames.org/findNearbyPlaceNameJSON",
            global: false,
            type: "GET",
            async: false,
            data: { lat: lonlat.lat, lng: lonlat.lon, username: username_api },
            success: function (response) {
                console.log(response);
                if (response.geonames.length != 0) {
                    var root = response.geonames;
                    OpenLayers.Util.getElement("coords").innerHTML = root[0].name + ", " + root[0].countryName + ", " + root[0].countryCode;
                }
                else {
                    OpenLayers.Util.getElement("coords").innerHTML = "Device Location cannot be inside sea";
                }

            },
            error: function (jqXHR, textStatus, errorThrown) {
                console.log(jqXHR.status);
                console.log(textStatus);
                console.log(errorThrown);
            }
        });

        /// Store the actual lat-lon 
        OpenLayers.Util.getElement("hidden-coords").innerHTML = lonlat.lat + " N, " + lonlat.lon + " E";

        

        /// Validate coords
        var isCoordsSet = OpenLayers.Util.getElement("coords").innerHTML;

        if (isCoordsSet == "Location" || isCoordsSet == "Device Location cannot be inside sea") {
            $("#coords").css("outline", "3px solid rgb(235, 46, 46)");
            $("#coords").css("margin-left", "3px");
            $("#coords").css("width", "94.5%");
        } else {
            $("#coords").css("outline", "");
            $("#coords").css("margin-left", "");
            $("#coords").css("width", "96%");
        }

    }

});
var map;
function init() {
    map = new OpenLayers.Map('map');

    var ol_wms = new OpenLayers.Layer.WMS("OpenLayers WMS",
        "http://vmap0.tiles.osgeo.org/wms/vmap0?", { layers: 'basic' });

    var jpl_wms = new OpenLayers.Layer.WMS("NASA Global Mosaic",
        "http://t1.hypercube.telascience.org/cgi-bin/landsat7",
        { layers: "landsat7" });

    jpl_wms.setVisibility(false);

    map.addLayers([ol_wms, jpl_wms]);
    map.addControl(new OpenLayers.Control.LayerSwitcher());
    // map.setCenter(new OpenLayers.LonLat(0, 0), 0);
    map.zoomToMaxExtent();

    var click = new OpenLayers.Control.Click();
    map.addControl(click);
    click.activate();

    ///Activate map Info
    OpenLayers.Util.getElement("mapInfo").style.display = "inline";
}